package com.jc.oms.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jc.common.util.WebUtils;
import com.jc.ds.base.service.BaseServiceException;
import com.jc.service.order.XorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("all")
@Controller
@RequestMapping("common")
public class CommonController {

    @RequestMapping(value = "/queryCommonList",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> queryCommonList(HttpServletRequest request){
        Map<String,Object> resultMap = new HashMap<String, Object>(){{
            put("code",10000);
        }};
        try {
            Map<String, Object> param = WebUtils.ReadAsChars(request);
            if (param.get("limit") == null){
                param.put("limit",10);
            }
            if (param.get("start") == null){
                param.put("start",1);
            }
            if (StringUtils.isEmpty(param.get("className"))){
                throw new BaseServiceException("方法名字不能为空");
            }
            Page<Object> objects = PageHelper.startPage(Integer.valueOf(param.get("start").toString()), Integer.valueOf(param.get("limit").toString()));
            String[] classNames = param.get("className").toString().split("\\.");
            String className = classNames[0];
            String method = classNames[1];
            WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
            Object xorderServiceImpl = wac.getBean(className);
            Class<?> cl = wac.getBean(className).getClass();
            Method queryAll = cl.getMethod(method,new Class[]{Map.class});
            Object invoke = queryAll.invoke(xorderServiceImpl, param);
            System.out.println(invoke);
            Map<String,Object> result = JSONObject.parseObject(JSONObject.toJSONString(invoke), Map.class);
            return result;
        } catch (Exception e) {
            resultMap.put("code","9999");
            e.printStackTrace();
        }
        return resultMap;
    }


    @RequestMapping(value = "/queryCommonInfo",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> queryCommonInfo(HttpServletRequest request){
        Map<String,Object> resultMap = new HashMap<String, Object>(){{
            put("code",10000);
        }};
        try {
            Map<String, Object> param = WebUtils.ReadAsChars(request);
            if (StringUtils.isEmpty(param.get("className"))){
                throw new BaseServiceException("方法名字不能为空");
            }
//            Page<Object> objects = PageHelper.startPage(Integer.valueOf(param.get("start").toString()), Integer.valueOf(param.get("limit").toString()));
            String[] classNames = param.get("className").toString().split("\\.");
            String className = classNames[0];
            String method = classNames[1];
            WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
            Object xorderServiceImpl = wac.getBean(className);
            Class<?> cl = wac.getBean(className).getClass();
            Method queryAll = cl.getMethod(method,new Class[]{Map.class});
            Object invoke = queryAll.invoke(xorderServiceImpl, param);
            System.out.println(invoke);
            resultMap.put("data",invoke);
        } catch (Exception e) {
            if (e instanceof InvocationTargetException) {
                Throwable targetEx =((InvocationTargetException)e).getTargetException();
                if (targetEx != null) {
                    resultMap.put("code","9999");
                    resultMap.put("msg", targetEx.getMessage());
                }
            } else {
                resultMap.put("code","9999");
                resultMap.put("msg", e.getMessage());
            }
        }
        return resultMap;
    }

@Autowired
private XorderService xorderService;

    @RequestMapping(value = "/queryOrder",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> queryOrder(HttpServletRequest request) throws UnsupportedEncodingException {
        Map<String, Object> stringObjectMap = WebUtils.ReadAsChars(request);
        Page<Object> objects = PageHelper.startPage(Integer.valueOf(stringObjectMap.get("start").toString()), Integer.valueOf(stringObjectMap.get("limit").toString()));
        Map<String, Object> stringObjectMap1 = xorderService.queryAllLimit(stringObjectMap);
        stringObjectMap1.put("code",10000);
        stringObjectMap1.put("msg","");
        return stringObjectMap1;
    }


    /**
     * 通过反射找到对应的service
     * */
//    public static Method findMethod(Class clazz, String name, Class[] paramTypes) {
//        Assert.notNull(clazz, "Class must not be null");
//        Assert.notNull(name, "Method name must not be null");
//        Class searchType = clazz;
//        while (!Object.class.equals(searchType) && searchType != null) {
//            Method[] methods = (searchType.isInterface() ? searchType.getMethods() : searchType.getDeclaredMethods());
//            for (int i = 0; i < methods.length; i++) {
//                Method method = methods[i];
//                if (name.equals(method.getName()) &&
//                        (paramTypes == null || Arrays.equals(paramTypes, method.getParameterTypes()))) {
//                    return method;
//                }
//            }
//            searchType = searchType.getSuperclass();
//        }
//        return null;
//    }

    //Map转Bean
//    public static <T> T mapToBean(Map<String, Object> map,T bean) {
//        BeanMap beanMap = BeanMap.create(bean);
//        beanMap.putAll(map);
//        return bean;
//    }
}
